<?php

namespace app\api\controller\wdsxh\mall;
use app\common\controller\Api;
use think\Db;

/**
 * 地址管理
 */
class Address extends Api
{
    protected $noNeedLogin = ['address_province','address_city','address_area'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //查询地址
    public function index()
    {
        $param = $this->request->param();
        //查询默认地址
        if(isset($param['is_default']) && !empty($param['is_default'])) {
            $where['is_default'] = array('eq',$param['is_default']);
        }
        $where['user_id'] = array('eq',$this->auth->id);
        $data = (new \app\api\model\wdsxh\mall\Address())
            ->where($where)
            ->field('id,name,tel,province,city,area,address,is_default')
            ->select();
        $this->success('请求成功',$data);

    }

    //添加地址
    public function add(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $default_id = (new \app\api\model\wdsxh\mall\Address())->where([
            'user_id'=>$user_id,
            'is_default'=>1,
        ])->value('id');
        if($param['is_default'] == 1 && $default_id) {
            Db::name('wdsxh_mall_user_address')->where('id',$default_id)->update([
                'is_default'=>0,
            ]);
        }
        Db::startTrans();
        try{
            Db::name('wdsxh_mall_user_address')->insert([
                'user_id'=>$user_id,
                'name'=>$param['name'],
                'tel' =>$param['tel'],
                'province'=>$param['province'],
                'city'=>$param['city'],
                'area'=>$param['area'],
                'address'=>$param['address'],
                'is_default'=>$param['is_default'] == true ? 1 : 0,
                'createtime'=>time(),
            ]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $id = (new \app\api\model\wdsxh\mall\Address())->where('user_id',$this->auth->id)->order('id desc')->value('id');
        $data = [
            'id'=>$id,
        ];
        $this->success('操作成功',$data);
    }

    //修改地址
    public function edit(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        if($param['is_default'] == true) {
            Db::name('wdsxh_mall_user_address')
                ->where('user_id',$user_id)
                ->update([
                    'is_default'=>0,
                ]);
        }
        Db::startTrans();
        try{
            Db::name('wdsxh_mall_user_address')
                ->where('id',$param['id'])
                ->update([
                    'name'=>$param['name'],
                    'tel' =>$param['tel'],
                    'province'=>$param['province'],
                    'city'=>$param['city'],
                    'area'=>$param['area'],
                    'address'=>$param['address'],
                    'is_default'=>$param['is_default'] == true ? 1 : 0,
                    'updatetime'=>time(),
                ]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('操作成功');
    }
    //删除地址
    public function del()
    {
        $userAddressObj =\app\api\model\wdsxh\mall\Address::get($this->request->param('id'));
        $userAddressObj->delete();
        $this->success('删除成功');
    }
    //修改默认地址
    public function set_default()
    {
        $id = $this->request->param('id');
        $user_id = $this->auth->id;
        Db::startTrans();
        try{
            Db::name('wdsxh_mall_user_address')
                ->where('user_id',$user_id)
                ->update([
                    'is_default'=>0,
                ]);
            Db::name('wdsxh_mall_user_address')
                ->where('id',$id)
                ->update([
                    'is_default'=>1,
                ]);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('操作成功');

    }


    //省
    public function address_province(){
        $count = Db::name('wdsxh_area')
            ->where('level',1)
            ->count();
        $data = Db::name('wdsxh_area')
            ->where('level',1)
            ->field('id,name')
            ->select();
        $this->success('请求成功',['count'=>$count,'data'=>$data]);
    }

    //市
    public function address_city(){
        $param = $this->request->param();
        $count = Db::name('wdsxh_area')
            ->where('level',2)
            ->where('pid',$param['crea_id'])

            ->count();
        $data = Db::name('wdsxh_area')
            ->where('level',2)
            ->where('pid',$param['crea_id'])
            ->field('id,name')
            ->select();
        $this->success('请求成功',['count'=>$count,'data'=>$data]);
    }

    //区
    public function address_area(){
        $param = $this->request->param();
        $count = Db::name('wdsxh_area')
            ->where('level',3)
            ->where('pid',$param['crea_id'])
            ->count();
        $data = Db::name('wdsxh_area')
            ->where('level',3)
            ->where('pid',$param['crea_id'])
            ->field('id,name')
            ->select();
        $this->success('请求成功',['count'=>$count,'data'=>$data]);
    }

}